package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    DishService dishService;
    @Autowired
    DishFlavorService dishFlavorService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    ShoppingCartService shoppingCartService;
    @Autowired
    RedisTemplate redisTemplate;


    @GetMapping("/page")
    public R<Page<DishDto>> page(Integer page, Integer pageSize, String name) {
        Page<DishDto> pages = dishService.selectPageByName(page, pageSize, name);
        return R.success(pages);
    }

    @GetMapping("/{id}")
    public R selectById(@PathVariable Long id) {
        DishDto dishDto = dishService.selectById(id);
        return R.success(dishDto);
    }

    @PostMapping
    public R save(@RequestBody DishDto dishDto) {
        dishService.saveWithFlavors(dishDto);
        redisTemplate.delete("dish_" + dishDto.getCategoryId());
        return R.success("保存成功");
    }

    @PutMapping
    public R update(@RequestBody DishDto dishDto) {
        dishService.updateById(dishDto);
        //现将所有的口味删掉
        dishFlavorService.remove(new QueryWrapper<DishFlavor>().eq("dish_id", dishDto.getId()));
        dishDto.getFlavors().stream().forEach(item -> {
            item.setDishId(dishDto.getId());
        });
        //再讲口味新增
        dishFlavorService.saveBatch(dishDto.getFlavors());
        redisTemplate.delete("dish_" + dishDto.getId());
        return R.success("修改成功");
    }

    @PostMapping("/status/{status}")
    public R updateStatusByIds(@PathVariable Integer status, Long[] ids) {
        dishService.updateStatusByIds(status, ids);
        return R.success("修改成功");
    }

    @DeleteMapping
    public R deleteByIds(Long[] ids) {
        dishService.deleteByIds(ids);
        return R.success("删除成功");
    }

    @GetMapping("/list")
    public R<List<DishDto>> list(Long categoryId, Integer status) {
        String key = "dish_" + categoryId;
        ListOperations listOperations = redisTemplate.opsForList();
        List<DishDto> list = listOperations.range(key, 0, -1);
        if (null == list || list.size() == 0) {
            list = dishService.listWithFlavors(categoryId, status);
            listOperations.rightPushAll(key, list);
        }
        return R.success(list);
    }
}
